package sanitycheck;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;

import pipeline.TriggerDetection;
import setting.BioNLPSettings;
import util.ling.Porter;
import abs.syn.Parse;
import def.Argument;
import def.BioNLPDocument;
import def.Event;
import exp.Count;

public class AnaphoricExpression {
	public static void main(String[] args){
		BioNLPSettings.init("dev.conf");
		
		TriggerDetection td = TriggerDetection.getTD();
		td.init();		
		
		
		String path = BioNLPSettings.getValue("dataPath");//"data/BioNLP09/dev/";
//		BioNLPSettings.parsePath = BioNLPSettings.devParsePath;
		Count sum = new Count();
		for (String filename :  new File(path).list()){
			//									String filename = "9159166.txt";
			if (filename.endsWith(".txt")){
				String filePrefix = filename.replace(".txt", "");
				System.out.println(filename+ " to be processed");
				sum.add(processDocument(path, filePrefix));
			}
			System.out.println(sum);
		}
	}


	public static Count processDocument(String path, String filePrefix){
		Count c = new Count();
		Porter stemmer = new Porter();
		HashSet<String> anaPhrases = new HashSet<String>();
		anaPhrases.add("its");
		anaPhrases.add("this");
		anaPhrases.add("that");
		anaPhrases.add("these");
		anaPhrases.add("those");
//		anaPhrases.add("the");
		anaPhrases.add("their");
		anaPhrases.add("it");
		anaPhrases.add("them");
		//		anaPhrases.add("its");
		//		anaPhrases.add("its");
		//		anaPhrases.add("its");
		//		anaPhrases.add("its");

		//				sum.add(processDocument(path, filename.replace(".txt", "")));
		BioNLPDocument doc = new BioNLPDocument(path, filePrefix);
		if (!doc.valid || doc.events==null)
			return c;
		for (int i =0 ; i <doc.aText.lines.length; i++){
			//FIXME delete
			//					if (i < 8)
			//						continue;
			//					if (i > 7)
			//						System.exit(-1);
			HashSet<Event> curEvents = new HashSet<Event>();
			System.err.println("====LINE: "+doc.aText.lines[i]+"========");
			// 3a. for each line of text
			// 3b. for each pair of trigger and protein, if it fits a pattern, add it in.
			Parse parse = doc.parses[i];
			int numTrigger =  0, numProtein = 0;
			int regNum = 0;

			for(Argument arg: doc.tspans.values()){
				// restrict to the current line
				if (arg instanceof Event && doc.aText.getSentenceIdByPos(arg.tspan.startIdx)==i){
					ArrayList<Integer> words = new ArrayList<Integer>();
					for (int j = 0; j < parse.words.length; j++){
						if (arg.tspan.startIdx - doc.aText.sentStartPos[i]>=parse.startPos[j] && arg.tspan.startIdx - doc.aText.sentStartPos[i]<=parse.endPos[j] 
                       ||arg.tspan.endIdx - doc.aText.sentStartPos[i]> parse.startPos[j]&&arg.tspan.endIdx - doc.aText.sentStartPos[i]<= parse.endPos[j]
					||arg.tspan.startIdx - doc.aText.sentStartPos[i]< parse.startPos[j] && arg.tspan.endIdx - doc.aText.sentStartPos[i]> parse.endPos[j]){
							words.add(j);
						}
					}
					if (words.size() == 0){
						System.err.println("[ANAPHORIC_ERROR] didn't find the word index!" + arg.tspan+" @ "+doc.aText.lines[i]);
					}
					else if (words.size() == 1){
						// TODO
						boolean hit = false;
						if (parse.leaves.get(words.get(0)).parent.getName().startsWith("V")){
							for (int j = 0; j < 1 && words.get(words.size()-1) + j+1 < parse.words.length; j++){
								if (anaPhrases.contains(parse.words[words.get(words.size() - 1)+j+1].toLowerCase())){
									System.err.println("[ANAPHORIC_FOUND] "+parse.words[words.get(0)]+"["+parse.leaves.get(words.get(0)).parent.getName()+"]"
											+" VERB contains context including " + parse.words[words.get(words.size() - 1)+j+1]);
									System.err.println("[ANAPHORIC_FOUND] "+doc.fileId+"@"+i+"  "+doc.aText.lines[i]);
									hit = true;
								}
							}
						}else if (parse.leaves.get(words.get(0)).parent.getName().startsWith("N")){
							for (int j = 0; j < 1 && words.get(words.size()-1) - j > 0; j++){
								if (anaPhrases.contains(parse.words[words.get(words.size() - 1)-j-1].toLowerCase())){
									System.err.println("[ANAPHORIC_FOUND] "+parse.words[words.get(0)]+"["+parse.leaves.get(words.get(0)).parent.getName()+"]"
											+" NOUN contains context including " + parse.words[words.get(words.size() - 1)-j-1]);
									System.err.println("[ANAPHORIC_FOUND] "+doc.fileId+"@"+i+"  "+doc.aText.lines[i]);
									hit = true;
								}
							}
						}else{
							for (int j = 0; j < 1 && words.get(words.size()-1) - j > 0; j++){
								if (anaPhrases.contains(parse.words[words.get(words.size() - 1)-j-1].toLowerCase())){
									System.err.println("[ANAPHORIC_FOUND] "+parse.words[words.get(0)]+"["+parse.leaves.get(words.get(0)).parent.getName()+"]"
											+" contains context LEFT including " + parse.words[words.get(words.size() - 1)-j-1]);
									System.err.println("[ANAPHORIC_FOUND] "+doc.fileId+"@"+i+"  "+doc.aText.lines[i]);
									hit = true;
								}
							}
							for (int j = 0; j < 1 && words.get(words.size()-1) + j+1 < parse.words.length; j++){
								if (anaPhrases.contains(parse.words[words.get(words.size() - 1)+j+1].toLowerCase())){
									System.err.println("[ANAPHORIC_FOUND] "+parse.words[words.get(0)]+"["+parse.leaves.get(words.get(0)).parent.getName()+"]"
											+" contains context including " + parse.words[words.get(words.size() - 1)+j+1]);
									hit = true;
									System.err.println("[ANAPHORIC_FOUND] "+doc.fileId+"@"+i+"  "+doc.aText.lines[i]);
								}
							}
						}

						if (!hit){
							System.err.println("[ANAPHORIC_NOT_FOUND] "+parse.words[words.get(0)]+"["+parse.leaves.get(words.get(0)).parent.getName()+"]");
						}
					}
					else if (words.size() > 1){
						String left = "";

						for (int j = 0; j < 3 && words.get(0) - j> 0; j++){
							left+= " "+parse.words[words.get(0)-j-1]+"["+parse.leaves.get(words.get(0)-j-1).parent.getName()+"]";
						}
						String right = "";
						for (int j = 0; j < 3 && words.get(words.size()-1) + j+1 < parse.words.length; j++){
							right+= " "+parse.words[words.get(words.size() - 1)+j+1]+"["+parse.leaves.get(words.get(words.size()-1)+j+1).parent.getName()+"]";
						}
						String self = "";
						for (int j = 0; j < words.size() ; j++){
							self+= " "+parse.words[words.get(j)]+"["+parse.leaves.get(words.get(j)).parent.getName()+"]";
						}
						System.err.println("[ANAPHORIC_MULTI] trigger in phrases. the linear context = {"+left+"}  "+self+" {"+right+"}");
					}
				}
			}
		}
		return c;
	}

}